package com.fingard.dsp.bank.directbank.nbcb01;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class NBCB01QryRefund extends NBCB01Base implements IQryRefund {
    @Override
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        retAts.writeStart();

        String tmpRespCode = "";
        String tmpRespInfo = "";

        int tmpSuccCount = 0;
        double tmpTotalAmt = 0;

        try {
            serviceId = "srv131_queryRefundTrade";
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按七天循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否大于七天
                if (DateHelper.getDifferDays(tmpCalendarTo.getTime(), tmpCalendarFrom.getTime()) > 6) {
                    tmpClndrStepTo.set(Calendar.DATE, 6);
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int tmpNowStartRecord = 0;//总条数
                int intPageNumber = 1;//页码数 默认查询第一页
                int tmpNowReadedCount = 0;//已读条数

                boolean tmpQryNext = true;
                while (tmpQryNext && tmpRespCode.length() == 0) {//分页查询
                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入报文头部信息
                    writeCommonHeader(xmlWriter);
                    if (!"".equals(bankFront.token)) {
                        xmlWriter.writeElementString("functionId", "srv131");
                        xmlWriter.writeElementString("functionName", "退汇来账列表查询");
                        xmlWriter.writeStartElement("opReq");
                        xmlWriter.writeElementString("serialNo", getQryTransID());
                        xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
                        xmlWriter.writeStartElement("ReqParam");
                        xmlWriter.writeElementString("ZHHA", p_req.payAct);
                        xmlWriter.writeElementString("KSSJ", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                        xmlWriter.writeElementString("JSSJ", Format.Date8Format.format(tmpCalendarTo.getTime()));
                        xmlWriter.writeElementString("XSYH", String.valueOf(intPageNumber));
                        xmlWriter.writeElementString("XSTS", "10");//推荐为10
                        xmlWriter.writeEndElement();
                        xmlWriter.writeEndElement();
                        String signXml = getDataSignData(xmlWriter);
                        if (signnatrue(signXml) == true) {
                            xmlWriter.writeElementString("signData", signature);
                            xmlWriter.writeEndDocument();
                            String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));
                            XmlTextReader tmpBkReader;
                            if (tmpStrRet[0].length() == 0) {
                                StringBuilder tmpSbMsg = new StringBuilder();
                                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                                if (tmpBkReader != null) {
                                    String returnCode = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                    String returnMsg = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                    if (isActiveRetCode(returnCode)) {
                                        int tmpTotalRocords = 0;//总条数
                                        if (tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/SHMU") != null && tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/SHMU").trim().length() > 0) {
                                            tmpTotalRocords = Integer.parseInt(tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/SHMU"));
                                        }
                                        int tmpEachPiCount = 0;
                                        while (tmpBkReader.read("/NBCBEBankData/opRep/list/row")) {
                                            tmpEachPiCount++;
                                            RetAtsRefundDetail tmpRetDetail = new RetAtsRefundDetail();
                                            String refundTicketDate = tmpBkReader.readedXElement.getSingleText("JYRQ");
                                            tmpRetDetail.refundTicketDate = refundTicketDate.substring(0, 4) + "-" + refundTicketDate.substring(4, 6) + "-" + refundTicketDate.substring(6);
                                            tmpRetDetail.amount = tmpBkReader.readedXElement.getSingleText("JYJE");
                                            tmpRetDetail.refundTicketReason = tmpBkReader.readedXElement.getSingleText("THYY");
                                            tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("SKZH");
                                            tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("FKZH");
                                            tmpRetDetail.askNum = tmpBkReader.readedXElement.getSingleText("XTLX");
                                            tmpSuccCount++;
                                            tmpTotalAmt += Double.parseDouble(tmpRetDetail.amount);

                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                        tmpNowReadedCount += tmpEachPiCount;//总记录数
                                        if (tmpNowReadedCount < tmpTotalRocords)//查询下一页
                                        {
                                            intPageNumber = intPageNumber + 1;//页码加一 查询第二页
                                            tmpNowStartRecord = tmpNowReadedCount + 1;//记录已经查询的记录个数
                                        } else if (tmpNowReadedCount == tmpTotalRocords)//本次分页查询完毕
                                        {
                                            tmpQryNext = false;
                                            tmpNowStartRecord = 0;
                                            tmpNowReadedCount = 0;
                                            continue;
                                        }
                                    } else {
                                        tmpQryNext = false;
                                        tmpRespCode = "-1";
                                        tmpRespInfo = returnMsg;
                                        break;
                                    }
                                } else {
                                    tmpQryNext = false;
                                    tmpRespCode = "-1";
                                    tmpRespInfo = tmpSbMsg.toString();
                                    break;
                                }
                            } else {
                                tmpQryNext = false;
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpStrRet[1];
                                break;
                            }
                        } else {
                            tmpQryNext = false;
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSign[1];
                            break;
                        }
                    } else {
                        tmpQryNext = false;
                        tmpRespCode = "-1";
                        tmpRespInfo = "登录失败";
                        break;
                    }
                }

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }

            retAts.totleAmount = String.valueOf(tmpTotalAmt);
            retAts.totleNum = String.valueOf(tmpSuccCount);
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if (tmpRespCode.equals("-1")) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("当前时间范围内无退票数据");
        }

        retAts.writeEnd();
        return retAts;
    }
}
